<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>axios的取消请求</h1>
    <button>发送请求</button>
    <button>取消请求</button>

    <script>
    //获取按钮
    let btns = document.querySelectorAll('button'); 
    //1. 声明外部变量
    let cancel;

    //绑定事件
    btns[0].onclick = async function(){
        let result = await axios.get(
            'http://localhost:3000/stu',
            {
            //2. 配置对象添加 cancelToken 属性
            cancelToken: new axios.CancelToken(c => {
                cancel = c;
            })
        });
        console.log(result.data);
    }

    btns[1].onclick = function(){
        //3. 调用 cancel 函数
        if(cancel){
            cancel();
        }
    }
    </script>
</body>
</html>